home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / radiance / simplerd.lha / simplerad / FinalFTP / Light / struct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-21  |  12.8 KB  |  422 lines

  1. /**********************************************************************/
  2. /* struct.c                                                           */
  3. /*                                                                    */
  4. /* Routines to print out structures, and writing final scene.         */
  5. /*                                                                    */
  6. /* Copyright (C) 1992, Bernard Kwok                                   */
  7. /* All rights reserved.                                               */
  8. /* Revision 1.0                                                       */
  9. /* May, 1992                                                          */
  10. /**********************************************************************/
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "geo.h"
  14. #include "misc.h"
  15. #include "io.h"
  16. #include "struct.h"
  17. #include "rad.h"
  18. #include "adj.h"
  19.  
  20. extern Vertex_tree *vtree;
  21. extern int vtree_size;
  22. extern RadParams ReadLog;
  23. extern void BoundsPrint();
  24. extern void Poly_VertexB();
  25. extern OptionType Option;
  26.  
  27. #define ON 1
  28. #define OFF 0
  29. FILE *logf;                        /* File for logging */
  30. char *logfilename = "out.scene";
  31. FILE *oscenef;                     /* File for scene output */
  32. char *oscenefilename = "out.scene";
  33.  
  34. /**********************************************************************/
  35. /* Print polygon and it's children                                    */
  36. /**********************************************************************/
  37. void print_Polygon(fp,label,p)
  38.      Polygon *p;
  39.      FILE *fp;
  40.      char *label;
  41. {
  42.   int i;
  43.  
  44.   fprintf(fp,"\tPolygon %s%d {\n", p->name, p->id);
  45.   fprintf(fp,"\ttype: %s; area: %g", 
  46.       (p->class == TRIANGLE ? "tri" : "quad"),
  47.       p->area);
  48.   fprintf(fp," level %d; chgnrm %d; number vertices %d, plane d %g\n",
  49.       p->level, p->changingNormal, p->numVert, p->d);
  50.   if (p->uvw != 0) {
  51.     fprintf(fp,"\tU(N=%g,%g,%g d=%g); V(N=%g,%g,%g d=%g);\n",
  52.         p->uvw->Nu.x, p->uvw->Nu.y,  p->uvw->Nu.z, p->uvw->du, 
  53.         p->uvw->Nv.x, p->uvw->Nv.y,  p->uvw->Nv.z, p->uvw->dv);
  54.     if (p->class == TRIANGLE) 
  55.       fprintf(fp,"\tW(N=%g,%g,%g d=%g)\n",
  56.           p->uvw->Nw.x, p->uvw->Nw.y,  p->uvw->Nw.z, p->uvw->dw);
  57.   }
  58.   fprintf(fp,"\tunshot B: ");
  59.   for(i=0;i<MAX_SPECTRA_SAMPLES;i++) 
  60.     fprintf(fp,"%g ", p->unshot_B.samples[i]);
  61.   fprintf(fp," total B: ");
  62.   for(i=0;i<MAX_SPECTRA_SAMPLES;i++) 
  63.     fprintf(fp,"%g ", p->B.samples[i]);
  64.   fprintf(fp,"\n");
  65.  
  66.   if (p->Mfather != 0)
  67.     fprintf(fp,"\tParent mesh: %s%d\n", p->Mfather->name, p->Mfather->id);
  68.   if (p->Pfather != 0)
  69.     fprintf(fp,"\tParent patch: %s\n", p->Pfather->name);
  70.   if (p->Links != 0) {
  71.     print_PolyList(fp, "of FFlinks", p->Links, 
  72.            p->polyhead.num_polys, 0);
  73.   }
  74.  
  75.   for (i=0;i<(p->numVert);i++) {
  76.     print_vector(fp, p->vert[i]->name, &(p->vert[i]->pos));
  77.     print_vector(fp, "Normal", &(p->normal[i]));
  78.   }
  79.   
  80.   for (i=0;i<MAX_PATCH_CHILDREN;i++)
  81.     if(p->child[i] != 0) {
  82.       fprintf(fp, "\tChild #%d\n", i);
  83.       print_Polygon(fp, "", p->child[i]);
  84.     }
  85.   fprintf(fp,"\t}\n");
  86. }
  87.  
  88. /*********************************************************************/
  89. /* Print list of polygons                                            */
  90. /*********************************************************************/
  91. void print_PolyList(fp, label, pl,plsize,full)
  92.      FILE *fp;
  93.      char *label;
  94.      PolyList *pl;
  95.      int plsize;
  96.      int full;
  97. {
  98.   PolyList *plptr;
  99.   int i;
  100.   
  101.   fprintf(fp, "\tPolylist %s {\n", label);
  102.   for(i=0, plptr= pl;i<plsize;i++, plptr=plptr->next) {
  103.     if(full) {
  104.       fprintf(fp,"\n");
  105.       print_Polygon(fp, "", plptr->patch);
  106.     } else 
  107.       fprintf(fp,"\tPolygon %s%d\n", plptr->patch->name, plptr->patch->id);
  108.   }
  109.   fprintf(fp,"\t}\n");
  110. }
  111.  
  112. /**********************************************************************/
  113. /* Print mesh                                                         */
  114. /**********************************************************************/
  115. void print_Mesh(fp, label,m)
  116.      Mesh *m;
  117.      FILE *fp;
  118.      char *label;
  119. {
  120.   Polygon *pptr;
  121.   int i;
  122.  
  123.   fprintf(fp,"\n\tMesh %s%d; %d polygon(s); Object: %s%d\n", 
  124.       m->name, m->id, m->num_polys, m->Ofather->name,
  125.       m->Ofather->id);
  126.   if (m->box != 0)
  127.     BoundsPrint(*(m->box), fp, m->name);
  128.   for (i=0, pptr = m->polys; i<m->num_polys; i++, pptr = pptr->next) {
  129.     print_Polygon(fp,"",pptr);
  130.   }
  131. }
  132.  
  133. /**********************************************************************/
  134. /* Print vertex                                                       */
  135. /**********************************************************************/
  136. void print_Vertex(fp,id,v)
  137.      FILE *fp;
  138.      int id;
  139.      Vertex *v;
  140. {
  141.   fprintf(fp,"\tVertex%d {\n", id);
  142.   print_vector(fp, "Position", &(v->pos));
  143.   
  144.   fprintf(fp,"\t%d adjacent polygons\n", v->polyhead.num_polys);
  145.   print_PolyList(fp, "of adjacent polyons", 
  146.          v->polylist, v->polyhead.num_polys,0);
  147.   fprintf(fp,"\t}\n");
  148. }
  149.  
  150. int vtx_count = 0;                    /* For labelling vertices */
  151. /**********************************************************************/
  152. /* Print tree of vertices                                             */
  153. /**********************************************************************/
  154. void print_Vertex_Tree(fp, label, vtptr)
  155.      FILE *fp;
  156.      char *label;
  157.      Vertex_tree *vtptr;
  158. {
  159.   int octant;
  160.  
  161.   if (vtptr != 0) { 
  162.     for (octant=0;octant<num_octants;octant++) 
  163.       if (vtptr->child[octant] != 0) 
  164.     print_Vertex_Tree(fp, label, vtptr->child[octant]);
  165.     print_Vertex(fp,vtx_count,vtptr->vtx);
  166.     vtx_count++;
  167.   }
  168. }
  169.     
  170. /**********************************************************************/
  171. /* Print a colour                                                     */
  172. /**********************************************************************/
  173. void print_Colour(fp, label, c)
  174.      FILE *fp;
  175.      char *label;
  176.      Colour *c;
  177. {
  178.   fprintf(fp,"\tColour %s: %g %g %g\n", label, c->r, c->g, c->b);
  179. }
  180.  
  181. /**********************************************************************/
  182. /* Print spectrum                                                     */
  183. /**********************************************************************/
  184. void print_Spectra(fp, label, s)
  185.      FILE *fp;
  186.      char *label;
  187.      Spectra s;
  188. {
  189.   int i;
  190.   fprintf(fp,"\t%s { ", label);
  191.   for (i=0;i<MAX_SPECTRA_SAMPLES;i++) fprintf(fp, "%g ",s.samples[i]);
  192.   fprintf(fp,"}\n");
  193. }
  194.  
  195. /**********************************************************************/
  196. /* Print shade properties */
  197. /**********************************************************************/
  198. void print_ShadeType(fp, label, st)
  199.      FILE *fp;
  200.      char *label;
  201.      ShadeType st;
  202. {
  203.   fprintf(fp,"\tShadetype %s\n", label);
  204.   print_Spectra(fp,"Ka",st.Ka);
  205.   print_Spectra(fp,"Kd",st.Kd);
  206.   print_Spectra(fp,"Ks",st.Ks);
  207.   print_Spectra(fp,"p",st.p);
  208.   print_Spectra(fp,"t",st.t);
  209.   print_Spectra(fp,"n",st.n);
  210. }
  211.  
  212. /**********************************************************************/
  213. /* Print energy                                                       */
  214. /**********************************************************************/
  215. void print_Radiance(fp, label, r)
  216.      FILE *fp;
  217.      char *label;
  218.      Radiance r;
  219. {
  220.   fprintf(fp,"\tRadiance %s\n", label);
  221.   print_Spectra(fp,"E",r.E);
  222.   print_Spectra(fp,"B",r.B);
  223. }
  224.  
  225. /**********************************************************************/
  226. /* Print surface properties                                           */
  227. /**********************************************************************/
  228. void print_SurfaceProp(fp, label, s)
  229.      FILE *fp;
  230.      char *label;
  231.      SurfaceProp *s;
  232. {
  233.   fprintf(fp,"\tSurfaceProp%d %s\n", s->id, s->name);
  234.   print_ShadeType(fp,label,s->shade);
  235.   /* print_Radiance(fp,label,s->rad); */
  236. }
  237.  
  238. /**********************************************************************/
  239. /* Print a texture map                                                */
  240. /**********************************************************************/
  241. void print_TextureMap(fp, label, t)
  242.      FILE *fp;
  243.      char *label;
  244.      TextureProp *t;
  245. {
  246.   int i,j;
  247.  
  248.   fprintf(fp,"\tTextureMap%d %s\n", t->id, t->name);
  249.   fprintf(fp,"\tclass %d; resolution %d\n", t->class, t->resolution);
  250.   for(i=0;i<t->resolution;i++) {
  251.     fprintf(fp,"\t");
  252.     for(j=0;j<t->resolution;j++) {
  253.       fprintf(fp,"%u ", *t->map);
  254.       t->map++;
  255.     }
  256.     fprintf(fp,"\n");
  257.   }
  258. }
  259.  
  260. /**********************************************************************/
  261. /* Print an object                                                    */
  262. /**********************************************************************/
  263. void print_Object(fp, label, n)
  264.      FILE *fp;
  265.      char *label;
  266.      Objectt *n;
  267. {
  268.   fprintf(fp,"\tObject%d %s\n", n->id, n->name);
  269.   fprintf(fp,"\trayID %d\n", n->rayID);
  270.   fprintf(fp,"\tprimtype[%d] =  %s\n", n->primid, n->primtype);
  271.   BoundsPrint(*(n->box), fp, n->name);
  272.   /* print_SurfaceProp(fp, "", n->surface); */
  273. }
  274.  
  275. /**********************************************************************/
  276. /* Switch log on / off                                                */
  277. /**********************************************************************/
  278. void Log(logflag,what)
  279.      int logflag;
  280.      char *what;
  281. {
  282.   if (logflag) {
  283.     if (!(logf = fopen(logfilename, "w"))) {
  284.       fprintf(stderr,"%s: cannot open log file %s\n", ProgName, logfilename);
  285.       exit(1);
  286.     } 
  287.     printf("\n\t*** Print log of %s to %s ***\n", what, logfilename);
  288.   } else 
  289.     fclose(logf);
  290. }
  291.  
  292.  
  293. /**********************************************************************/
  294. /* Print viewing position */
  295. /**********************************************************************/
  296. void print_View(fp,c)
  297.      FILE *fp;
  298.      Camera *c;
  299. {
  300.   fprintf(fp,"\tCamera {\n");
  301.  
  302.   fprintf(fp,"\t\tlookfrom %g %g %g\n",
  303.       c->lookfrom.x, c->lookfrom.y, c->lookfrom.z);
  304.   fprintf(fp,"\t\tlookat %g %g %g\n", 
  305.       c->lookat.x,c->lookat.y,c->lookat.z);
  306.   fprintf(fp,"\t\tlookup %g %g %g\n",
  307.       c->lookup.x,c->lookup.y, c->lookup.z);
  308.   fprintf(fp,"\t\tfovx %d fovy %d near %g far %g\n",
  309.       c->fovx, c->fovy,c->near, c->far);
  310.   fprintf(fp,"\t\txRes %d yRes %d\n", c->xRes, c->yRes);
  311.   fprintf(fp,"\t}");
  312. }
  313.  
  314. /**********************************************************************/
  315. /* Print the scene out to log file */
  316. /**********************************************************************/
  317. void print_Scene(s, fname)
  318.      Scene *s;
  319.      char *fname;
  320. {
  321.   int i,j;
  322.   Objectt *optr;
  323.   Mesh *mptr;
  324.  
  325.   /* Print all polygons, and vertices */
  326.   logfilename = fname;
  327.  
  328.   Log(ON,"scene");
  329.   optr = s->objects;
  330.   for(i=0;i<s->num_objects;i++,optr++) {
  331.     mptr = optr->meshes;
  332.     print_Object(logf,"",optr);
  333.     for (j=0;j<optr->num_meshes;j++, mptr++) 
  334.       print_Mesh(logf,"blah",mptr);
  335.   }
  336.  
  337.   vtx_count = 0;
  338.   fprintf(logf,"\n\tVertexTree size: %d {\n", vtree_size);
  339.   print_Vertex_Tree(logf, "", vtree);
  340.   Log(OFF,"");
  341.  
  342.   /* Print display view */
  343.   /* logfilename = "log.view";
  344.      Log(ON,"view");
  345.      print_View(logf,&(ReadLog.displayView));
  346.      Log(OFF,""); */
  347. }
  348.  
  349. /**********************************************************************/
  350. /* Output results after running radiosity on scene                    */
  351. /**********************************************************************/
  352. void Write_Rad(rparams, filename, num)
  353.      RadParams rparams;
  354.      char *filename;
  355.      int num;
  356. {
  357.   int i,j,k;
  358.   Elist *elptr;
  359.   Polygon *eptr;
  360.   FILE *fp;
  361.   char ffilename[80];
  362.   Colour c[MAX_PATCH_VTX];
  363.  
  364.   if (num > 0) 
  365.     sprintf(ffilename,"%s%d", filename, num);
  366.   else
  367.     sprintf(ffilename,"%s", filename);
  368.   if (!(fp = fopen(ffilename, "w"))) {
  369.     fprintf(stderr,"%s: cannot open results file %s\n", ProgName, ffilename);
  370.     exit(1);
  371.   } 
  372.   printf("\n\t*** Printing %d polygons to %s ***\n", 
  373.      rparams.num_elements, ffilename);
  374.   
  375.   /* Print elements to file */
  376.   fprintf(fp,"Number polygons %d\n", rparams.num_elements);
  377.   elptr = rparams.elements;
  378.   for(i=0; i<rparams.num_elements;i++, elptr = elptr->next) {
  379.     eptr = elptr->element;
  380.  
  381.     /* Print patch label */
  382.     fprintf(fp,"Poly %s%d %d {\n", "poly", i, 
  383.         eptr->numVert);
  384.  
  385.     /* Print colour of patch (flat shade it for now) */
  386.     fprintf(fp,"  B { ");
  387.     if (Option.rad_interp_type == INTERP_VTX_FROM_PATCH) {
  388.       for (k=0;k<MAX_SPECTRA_SAMPLES;k++) 
  389.     fprintf(fp,"%g ", 
  390.         (eptr->B.samples[k] > 1.0 ? 1.0 : eptr->B.samples[k]));
  391.     } else {
  392.       for (k=0;k<MAX_SPECTRA_SAMPLES;k++) fprintf(fp,"-1.0 ");
  393.     }      
  394.     fprintf(fp,"}\n");
  395.  
  396.     /* Find and print colour of vertices */
  397.     Poly_VertexB(eptr, c);
  398.     fprintf(fp,"  vtx_B { ");
  399.     for (j=0;j<eptr->numVert;j++)
  400.       fprintf(fp,"{ %g %g %g } ", c[j].r, c[j].g, c[j].b);
  401.     fprintf(fp,"}\n");
  402.     
  403.     /* Print normals */
  404.     fprintf(fp,"  norm {",i);
  405.     for (j=0;j<eptr->numVert;j++) 
  406.       fprintf(fp," { %g %g %g } ",
  407.          eptr->normal[j].x, eptr->normal[j].y, eptr->normal[j].z);
  408.     fprintf(fp,"}\n");
  409.     
  410.     /* Print vertices */
  411.     fprintf(fp,"  vert {",i);
  412.     for (j=0;j<eptr->numVert;j++) 
  413.       fprintf(fp," { %g %g %g } ", eptr->vert[j]->pos.x, 
  414.           eptr->vert[j]->pos.y, eptr->vert[j]->pos.z);
  415.     fprintf(fp,"}\n");
  416.  
  417.     fprintf(fp,"}\n");
  418.   }
  419.  
  420.   fclose(fp);
  421. }
  422.